<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>ADL bridging - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/hooks/keeping_state.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/advanced/hooks.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/hooks/hook_result.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">ADL bridging</h1></div></div></div>
  <div class="notices note" style="background: url('../../../images/note.png') top left no-repeat padding-box padding-box;">
<div class="notices heading">note</div>
<div class="notices message"><p>In Outcome v2.2 the ADL-based event hooks were replaced with policy-based event hooks (next page).
The code in this section is still valid in v2.2 onwards, it&rsquo;s just that ADL is no longer used
to find the hooks.</p>
</div>
</div>


<p>In a previous section, we used the <code>failure_info</code> type to create
the ADL bridge into the namespace where the ADL discovered <a href="../../../reference/functions/policy.html"><code>outcome_throw_as_system_error_with_payload()</code></a> function was to be found.</p>

<p>Here we do the same, but more directly by creating a thin clone of <code>std::error_code</code>
into the local namespace. This ensures that this namespace will be searched by the
compiler when discovering the event hooks (Outcome v2.1 and earlier only).</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">namespace</span> <span class="n">error_code_extended</span>
<span class="p">{</span>
  <span class="c1">// Use the error_code type as the ADL bridge for the hooks by creating a type here
</span><span class="c1"></span>  <span class="c1">// It can be any type that your localised result uses, including the value type but
</span><span class="c1"></span>  <span class="c1">// by localising the error code type here you prevent nasty surprises later when the
</span><span class="c1"></span>  <span class="c1">// value type you use doesn&#39;t trigger the ADL bridge.
</span><span class="c1"></span>  <span class="k">struct</span> <span class="nl">error_code</span> <span class="p">:</span> <span class="k">public</span> <span class="n">std</span><span class="o">::</span><span class="n">error_code</span>
  <span class="p">{</span>
    <span class="c1">// literally passthrough
</span><span class="c1"></span>    <span class="k">using</span> <span class="n">std</span><span class="o">::</span><span class="n">error_code</span><span class="o">::</span><span class="n">error_code</span><span class="p">;</span>
    <span class="n">error_code</span><span class="p">()</span> <span class="o">=</span> <span class="k">default</span><span class="p">;</span>
    <span class="n">error_code</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">error_code</span> <span class="n">ec</span><span class="p">)</span>
        <span class="o">:</span> <span class="n">std</span><span class="o">::</span><span class="n">error_code</span><span class="p">(</span><span class="n">ec</span><span class="p">)</span>
    <span class="p">{</span>
    <span class="p">}</span>
  <span class="p">};</span>

  <span class="c1">// Localise result and outcome to using the local error_code so this namespace gets looked up for the hooks
</span><span class="c1"></span>  <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="err"> </span><span class="nc">R</span><span class="o">&gt;</span> <span class="k">using</span> <span class="n">result</span> <span class="o">=</span> <span class="n">BOOST_OUTCOME_V2_NAMESPACE</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="n">R</span><span class="p">,</span> <span class="n">error_code</span><span class="o">&gt;</span><span class="p">;</span>
  <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="err"> </span><span class="nc">R</span><span class="o">&gt;</span> <span class="k">using</span> <span class="n">outcome</span> <span class="o">=</span> <span class="n">BOOST_OUTCOME_V2_NAMESPACE</span><span class="o">::</span><span class="n">outcome</span><span class="o">&lt;</span><span class="n">R</span><span class="p">,</span> <span class="n">error_code</span> <span class="cm">/*, std::exception_ptr */</span><span class="o">&gt;</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/error_code_extended.cpp#L95" class="code-snippet-url" target="_blank">View this code on Github</a></div>


<p>For convenience, we template alias local copies of <code>result</code> and <code>outcome</code> in this
namespace bound to the ADL bridging <code>error_code</code>.</p>


        </div><p><small>Last revised: December 15, 2020 at 12:22:39 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/hooks/keeping_state.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/advanced/hooks.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/hooks/hook_result.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
</html>
